package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.pojo.PageDTO;
import com.hmall.item.config.MqConstants;
import com.hmall.item.exception.BusinessException;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 查询所有
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> page(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        Page<Item> itemPage = new Page<>(page, size);
        itemService.page(itemPage, null);
        PageDTO<Item> pageDto = new PageDTO<>();
        pageDto.setTotal(itemPage.getTotal());
        pageDto.setList(itemPage.getRecords());
        return pageDto;
    }

    /**
     * 通过id查商品
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Item getById(@PathVariable Long id) {
        if (id == null) {
            throw new BusinessException("id不能为空");
        }
        return itemService.getById(id);
    }

    /**
     * 新增商品
     *
     * @param item
     */
    @PostMapping
    public void saveItem(@RequestBody Item item) {
        if (item == null) {
            throw new BusinessException("添加参数不能为空");
        }
        itemService.save(item);
    }

    /**
     * 商品的上下架
     *
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void soldOut(@PathVariable("id") Long id, @PathVariable("status") Integer status) {

        Item one = itemService.getById(id);
        if (one != null) {
            one.setStatus(status);
            itemService.updateById(one);
 
            if ( status.equals(1)) {
                rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE, MqConstants.ITEM_INSERT_KEY, one.getId());
            } else if ( status.equals(2)){
                rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE, MqConstants.ITEM_DELETE_KEY, one.getId());
            }

        }
    }

    /**
     * 修改
     *
     * @param item
     */
    @PutMapping
    public void updateItem(@RequestBody Item item) {
        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Item::getId, item.getId());
        itemService.update(item, wrapper);
    }

    /**
     * 删除
     *
     * @param id
     */
    @DeleteMapping({"/{id}"})
    public void removeById(@PathVariable("id") Long id) {
        itemService.removeById(id);
    }



    @PutMapping("/stock/{itemId}/{num}")
    public void reduceInventory(@PathVariable("itemId")Long id,@PathVariable("num")Integer num){
        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(id!=null, Item::getId,id);
        Item one = itemService.getOne(wrapper);
        one.setStock(one.getStock() -  num );
        boolean update = itemService.updateById(one);
        System.out.println("item减库存成功与否 = " + update);
    }
}
